//============================================================================
// Name        : Trainer3D.cpp
// Author      : alan
// Version     :
// Copyright   : BRT - ACSO
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
using namespace std;

#include <iostream>
#include <string>
//Bibliotecas para leitora de configuracao

#include "Utilidade/include/Properties.h"
#include "Utilidade/include/PropertyReader.h"
#include "Utilidade/include/Property.h"
//#include "Utilidade/include/Roboviz.h"
//Bibliotecas para log
#include "Logger/include/Logger.h"

#include "Conexao/include/ConexaoTCP.h"
#include "Conexao/include/Exceptions/SocketExceptions.hpp"
#include "Trainer/include/Trainer.h"
#include <thread>


void configure_logger(bool enabled, std::string level)
{
    LogParameters& l_params = LogParameters::instance();

    if (enabled == true)
    {
        l_params.active(true);
    }
    else
    {
        l_params.active(false);
    }

    if (level == "DEBUG")
    {
        l_params.level(log_DEBUG);
        LOG(log_INFO) << "Logger initialized to level debug";
    }
    else if (level == "INFO")
    {
        l_params.level(log_INFO);
        LOG(log_INFO) << "Logger initialized to level info";
    }
    else if (level == "WARN")
    {
        l_params.level(log_WARN);
        LOG(log_INFO) << "Logger initialized to level warn";
    }
    else if (level == "ERROR")
    {
        l_params.level(log_ERROR);
        LOG(log_INFO) << "Logger initialized to level error";
    }
    else
    {
        l_params.level(log_WARN);

        LOG(log_WARN) << "Invalid log level " << level;
    }
}

int main(int argc, char *argv[]) {

	PropertyReader propsReader;
	//Properties props = propsReader.read("../configTrainer.ini");
	Properties props = propsReader.read("../config.ini");//usando o bin

	bool enable = false;
	std::string ang;
	std::string dist;
	std::string exp;
	//std::string pos;
	std::string time;
	std::string ip;
	std::string lerarq;
    std::string listDadosJogo;

	double timeMax;
	int distancia;
	double angulo;
	int experimento;
	double posicao;



	dist = props.Get("DISTANCE");//DISTANCIA DA BOLA E AGENTE
	ang = props.Get("ANGULO");//ANGULO ENTRE A BOLA E AGENTE
	exp = props.Get("EXPERIMENTO");
	//pos = props.Get("POSICAO_X_AGENTE");
	time = props.Get("TEMPO_MAXIMO");
	ip = props.Get("IP_CONNECT_TRAINER");
    lerarq =props.Get("LOCAL_LOG");//ENDEREÇO DO ARQUIVO DE LOG ROBOVIZ
    listDadosJogo=props.Get("LISTA_DADOS_SIMULACAO");



	distancia = convert_to<int>(dist);
	angulo = convert_to<double>(ang);
	experimento = convert_to<int>(exp);
	//posicao = convert_to<double>(pos);
	timeMax = convert_to<double>(time);

	if(props.Get("LOG_ENABLED") == "YES")
		{
			enable = true;
		}



    if(props.Get("ARQ_LOG")=="YES"|| argv[1]!=NULL){

    	std::string param;

     if(argv[1]){
    	param= argv[1];
      }else  param= lerarq;

        Trainer trainer(param);
        trainer.setListaDadosJogo(listDadosJogo);
		trainer.setTempoMaximo(timeMax);
		trainer.leitorArquivo();

	}else{

		           Trainer trainer(ip,"3200");

					trainer.setDistancia(distancia);
					trainer.setAnguloAgenteBola(angulo);
					trainer.setTipo(experimento);
					//trainer.setPosicaoXAgente(posicao);
					trainer.setTempoMaximo(timeMax);
					trainer.setListaDadosJogo(listDadosJogo);
				   // std::thread trd(std::ref(trainer));

					trainer.run();

	}


}



